package mt;

import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author zhangmin
 * @create 2022-03-16 21:05
 */
public class meituan2021_1_4 {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        int res=0;
        LinkedList<Integer> list=new LinkedList<>();
        for (int i = 0; i < n; i++) {
            int cur=cin.nextInt();
            while(list.size()!=0&&list.getLast()==cur){
                res++;
                cur=list.removeLast()+1;
            }
            list.addLast(cur);
        }
        System.out.println(res);
    }
}
